/**
 * Copyright (C) 2008 University of Pittsburgh
 * 
 * 
 * This file is part of Open EpiCenter
 * 
 *     Open EpiCenter is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     Open EpiCenter is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with Open EpiCenter.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 
 *   
 */
package com.hmsinc.epicenter.model.health;

// Generated Jan 15, 2007 10:58:01 AM by Hibernate Tools 3.2.0.beta8

import static javax.persistence.GenerationType.AUTO;

import java.util.SortedSet;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;

import com.hmsinc.epicenter.model.provider.Facility;

/**
 * Patient generated by hbm2java
 */
@Entity
@Table(name = "PATIENT")
@org.hibernate.annotations.Table(appliesTo = "PATIENT", indexes = { @org.hibernate.annotations.Index(name = "IDX_PATIENT_1", columnNames = "FACILITY_PATIENT_IDEN"),
	@org.hibernate.annotations.Index(name = "IDX_PATIENT_2", columnNames = "ID_FACILITY")})
@NamedQueries( { @NamedQuery(name = "getPatient", query = "from Patient p where p.facility = :facility and p.patientId = :patientId"),
	@NamedQuery(name = "exportPatient", query = "from Patient p left join fetch p.facility left join fetch p.patientDetails d left join fetch p.interactions h left join fetch h.classifications where p.id < 100000 order by p.id")})
public class Patient implements HealthObject {

	// Fields

	/**
	 * 
	 */
	private static final long serialVersionUID = 5652166296784746970L;

	private Long id;

	private String patientId;

	private Facility facility;

	private SortedSet<PatientDetail> patientDetails = new TreeSet<PatientDetail>();

	private SortedSet<Interaction> interactions = new TreeSet<Interaction>();

	// Constructors

	/** default constructor */
	public Patient() {
	}

	/**
	 * @param facility
	 */
	public Patient(Facility facility) {
		super();
		this.facility = facility;
	}

	/**
	 * @param patientId
	 * @param facility
	 */
	public Patient(String patientId, Facility facility) {
		super();
		this.patientId = patientId;
		this.facility = facility;
	}


	/** full constructor */
	public Patient(String patientId, Facility facility, SortedSet<PatientDetail> patientDetails,
			SortedSet<Interaction> interactions) {

		this.patientId = patientId;
		this.facility = facility;
		this.patientDetails = patientDetails;
		this.interactions = interactions;
	}

	// Property accessors
	@GenericGenerator(name = "generator", strategy = "native", parameters = { @Parameter(name = "sequence", value = "SEQ_PATIENT") })
	@Id
	@GeneratedValue(strategy = AUTO, generator = "generator")
	@Column(name = "ID", nullable = false, insertable = true, updatable = true)
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "ID_FACILITY", unique = false, nullable = false, insertable = true, updatable = true)
	@org.hibernate.annotations.ForeignKey(name = "FK_PATIENT_1")
	@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
	public Facility getFacility() {
		return this.facility;
	}

	public void setFacility(Facility facility) {
		this.facility = facility;
	}

	@Column(name = "FACILITY_PATIENT_IDEN", unique = false, nullable = true, insertable = true, updatable = true, length = 32)
	public String getPatientId() {
		return this.patientId;
	}

	public void setPatientId(String patientId) {
		this.patientId = patientId;
	}

	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "patient")
	@Sort(type = SortType.NATURAL)
	public SortedSet<PatientDetail> getPatientDetails() {
		return this.patientDetails;
	}

	public void setPatientDetails(SortedSet<PatientDetail> patientDetails) {
		this.patientDetails = patientDetails;
	}

	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "patient")
	@Sort(type = SortType.NATURAL)
	public SortedSet<Interaction> getInteractions() {
		return this.interactions;
	}

	public void setInteractions(SortedSet<Interaction> interactions) {
		this.interactions = interactions;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return new HashCodeBuilder(13, 47).append(getId()).append(getFacility()).append(getPatientId()).toHashCode();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object o) {

		boolean ret = false;
		if (o instanceof Patient == false) {
			ret = false;
		} else if (this == o) {
			ret = true;
		} else {
			final Patient ag = (Patient) o;
			ret = new EqualsBuilder().append(getId(), ag.getId()).append(getFacility(), ag.getFacility()).append(getPatientId(),
					ag.getPatientId()).isEquals();
		}
		return ret;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return new ToStringBuilder(this).append("id", id).append("patientId", patientId).append("facility", (facility == null ? null : facility.getIdentifier())).toString();
	}
	
}
